discard

set more off 


/** DISPUTES **/
	
 **     this do-file accepts/rejects all disputes
 ** 		(apart from ones solved in the previous dofile 
 ** 		 that dealt with ff errors) 
 
 ** 	rules are: 
 **				(1) accept dispute if only one report of condition 
 **					(disputed one) and no medication reported
 **								or misdiagnosis reported   
 **			    (2) reject dispute if two or more reports of condition 
 **					     (including medication) 
 **					and misdiagnosis not reported 

 ** 				note in wave 2 one cannot report a misdiagnosis 
 
  use "$healthsave\with ff errors solved_w7.dta", clear 
 set more off 
 lab define howcl 5 "Dispute accepted (1 report)" 		///
				  6 "Dispute accepted (misdiagnosis)"  	///
				  7 "Dispute rejected" 					///
				  8 "Dispute rejected (meds)"  			///
				  9 "Dispute rejected (not prev)" , modify


/** construct useful indicators **/	

	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	
	** indicator for whether a wave is disputed (combined with ever`x'_w?) 
		
		egen disputed`x'_w0     = anymatch(disp`x'_w2 disp`x'_w3 disp`x'_w4  disp`x'_w5 disp`x'_w6 disp`x'_w7) , v(3 4 5) 				
		cap egen disputed`x'_w1 = anymatch(disp`x'_w2 disp`x'_w3 disp`x'_w4  disp`x'_w5 disp`x'_w6 disp`x'_w7) , v(3 4 5) 		
		cap egen disputed`x'_w2 = anymatch(			  disp`x'_w3 disp`x'_w4  disp`x'_w5 disp`x'_w6 disp`x'_w7) , v(3 4 5) 
		cap egen disputed`x'_w3 = anymatch(           			 disp`x'_w4  disp`x'_w5 disp`x'_w6 disp`x'_w7) , v(3 4 5) 
		cap egen disputed`x'_w4 = anymatch(			  				         disp`x'_w5 disp`x'_w6 disp`x'_w7) , v(3 4 5) 
		cap egen disputed`x'_w5 = anymatch(								    			disp`x'_w6 disp`x'_w7) , v(3 4 5) 
		cap egen disputed`x'_w6 = anymatch(												disp`x'_w6 disp`x'_w7) , v(3 4 5)
		cap egen disputed`x'_w7 = anymatch(disp`x'_w7)														   , v(3   5)
	
		cap egen everdispute`x' = anymatch(disp`x'_w?) , v(3 4 5) 
		
	}
				  
				
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		forv i = 0/7 { 
			
			gen dts`x'_w`i' = disputed`x'_w`i' 
			replace dts`x'_w`i' = . if ffproblem`x' == 1 | alive_w`i' == 0 | totdisp`x' >1 
			lab var dts`x'_w`i' "Dispute to solve in this section" 
		}
	}
	
  
  /** for documentation **/
  
	cd "P:\ELSA\Healthdynamics\documentation\tables\" 
	
	preserve 
	pause on
	local fortabout 
	local fortabout2 
	local fortabout3 
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	
		egen disp`x' = anymatch(disputed`x'_w?) , v(1) 
		egen anyff`x' = anymatch(ff`x'_w?) , v(1) 
		
		replace disp`x' = . if ffproblem`x' == 1 
		replace anyff`x' = . if ffproblem`x' == 1 
		
		replace disp`x' = . if anyff`x' == 0 
		
		local fortabout `fortabout' mean disp`x' 
		local fortabout2 `fortabout2' count disp`x' 
		local fortabout3 `fortabout3' sum disp`x' 
		
	} 
	
	gen n = 1 
	tabout n using dispute_rate.txt , ///
			c(`fortabout') sum replace f(4)  
	tabout n using number_ever_ff_properly.txt , ///
			c(`fortabout2') sum replace f(4)  	
	tabout n using number_disputes_after_proper_ff.txt , ///
			c(`fortabout3') sum replace f(4)    
			
	restore 
				  
				  
				  
/**********************************************/
/** 1. Accept a dispute if only report of    **/
/**  health condition (including medication) **/		
/**         or misdiagnosis reported.        **/
/**********************************************/

foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		
		/** replace report equal to zero if 
			dispute in subsequent wave accepted 
			[no reports in wave 0 disputed] **/ 

	forv i=1/6 {
		local Ni = `i'+1
	
		replace ever`x'_w`i'= 0 ///
			if dts`x'_w`i' == 1 ///
				& ((disp`x'_w`Ni'==3 			///	  /* disputed */
					& nrep`x'_w`i'==1 & nmed`x'_w`i'==0)    ///   /* not many reports */
				| disp`x'_w`Ni' == 5 )  				      /* or misdiagnosed */
					
		replace howcl`x'_w`i' = 5  ///
			if dts`x'_w`i' == 1    ///
				& (disp`x'_w`Ni'==3 	///	/* disputed */
				& nrep`x'_w`i'==1 & nmed`x'_w`i'==0)  
	
	/* misdiagnoses */			
		replace howcl`x'_w`i' = 6 ///
			if dts`x'_w`i' == 1   ///
			& disp`x'_w`Ni' == 5  
	}
}
			
foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	forv i=2/6 {
		local Pi = `i'-1			
			
	/* label contemporaneous acceptances of disputes */
		replace howcl`x'_w`i' = 6 	///
			if dts`x'_w`i' == 1  	///
			& ever`x'_w`i' == 0 	///
			& disp`x'_w`i' == 5 
			
		replace howcl`x'_w`i' = 5 	///
			if dts`x'_w`i' == 1  	///
				& (disp`x'_w`i'==3 	///	  
				& nrep`x'_w`Pi'==1 & nmed`x'_w`Pi'==0)  
		
	}
}

/**  there may be some reports further back to be corrected. **/

	** eg 1 | 1 d 0 0 0 has already become 1 | 0 0 0 0 0 
	** this step changes it to 0 | 0 0 0 0 0 
 	
	order disp* , sequential 
	
foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	
	forv i = 0/6 { 
		egen evermisd`x'`i' = anymatch(disp`x'_w`i' - disp`x'_w7) , v(5) 
		egen everacc`x'`i' = anymatch(howcl`x'_w`i' - howcl`x'_w7) , v(5) 
		replace evermisd`x'`i' = 1 if everacc`x'`i' == 1 
		drop everacc`x'`i' 
		replace evermisd`x'`i' = . if dts`x'_w`i' == . 
		replace ever`x'_w`i'  = 0  if 	evermisd`x'`i' == 1 
		replace howcl`x'_w`i' = 5  if   evermisd`x'`i' == 1 	
		*drop evermisd`x'`i' 
	}
}
	
	
	
**************************************************************	
** 2. reject a dispute if... 								**
** 		1. two or more reports of the condition			 	**
**				(including disputed wave) 					**
**		   	and no report of misdiagnosis 					**
**															** 
**		2. gives as reason for dispute 						** 
**			'didn't have it before but have it now' 		**
** 															**
**		2. medicating (count this as an extra report, to 	**
**		    count this towards maximum two reports) 		**
**  		must not be report of misdiagnosis. 			**
**************************************************************


foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 

	forv i=1/7 {			  /* let i be the wave to be corrected */
		forv w = 1/7 { 		  /* and w be the wave of the dispute */
			local Pw = `w'-1 
			
		if `i' < `w'   { 	/* wave to be corrected comes BEFORE dispute */
			
		replace ever`x'_w`i'=1 			 					 ///			
				if nrep`x'_w`i'>=1 			/// /* already reported condition by now */
				& dts`x'_w`i' ==1			/// /* unsolved */
				&     (disp`x'_w`w' == 3 	/// /* never had it */ 
						& (nrep`x'_w`Pw'>1 	| ( nrep`x'_w`Pw'>=1 & nmed`x'_w`Pw'>=1)) ///
					|  disp`x'_w`w'==4 ) 	/* didn't have it before, have now */ 
							
		replace howcl`x'_w`i' = 7 ///
				if  nrep`x'_w`i'>=1 &	dts`x'_w`i' ==1		 ///
				&     disp`x'_w`w' == 3 &	nrep`x'_w`Pw'>1
						
					
		replace howcl`x'_w`i' = 8 ///
				if nrep`x'_w`i'>=1 ///
				& 	  dts`x'_w`i' ==1		 ///
				&   ( nrep`x'_w`w'>=1 & nmed`x'_w`w'>=1 & disp`x'_w`w' == 3 ) 
		
		replace howcl`x'_w`i' = 9 ///
				if  nrep`x'_w`i'>=1 & dts`x'_w`i' ==1		 ///
					&  disp`x'_w`w'==4		
		} 
		
		else { 				/* wave to be correct is AFTER the dispute 
									(feed rejections forward) */	
		
		replace ever`x'_w`i'=1 			 ///			
			if ffproblem`x' == . & alive_w`i' == 1 & totdisp`x'==1 ///
				&  ((disp`x'_w`w' == 3  &  ( nrep`x'_w`Pw'>1 | ( nrep`x'_w`Pw'>=1 & nmed`x'_w`Pw'>=1))) ///
			      |  disp`x'_w`w'==4) 
		
		replace howcl`x'_w`i' = 7 /// 
				if ffproblem`x' == . & alive_w`i' == 1 & totdisp`x'==1 ///
				&  disp`x'_w`w' == 3  & nrep`x'_w`Pw'>=2 
			       
		
		replace howcl`x'_w`i' = 8 /// 
				if ffproblem`x' == . & alive_w`i' == 1 & totdisp`x'==1 ///
				&  disp`x'_w`w' == 3 &  nrep`x'_w`Pw'>=1 & nmed`x'_w`Pw'>=1	
		
		replace howcl`x'_w`i' = 9 /// 
				if ffproblem`x' == . & alive_w`i' == 1 & totdisp`x'==1 ///
				&  disp`x'_w`w'==4 
			}
		}
	}
}
	


	
/*************************/
/*** multiple disputes ***/
/*************************/

** can use indicator for multiple disputes and manually re-code. 

** problem arises because when multiple disputes, might accept first dispute
** but then reject second on basis of original report. Inconsistent treatment of
** first report - is it true or false? 

** treat the first report as true, so reject both disputes. 

 pause on 

foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
di in red "`x'" 
count if totdisp`x'>=2  
if `r(N)' >=2 { 
	**br idauniq ever`x'* disp`x'* med`x'* if totdisp`x'>=2 & totdisp`x'!=. & ffproblem`x'!=1 
	*pause 	 
	}
}

** the following conditions have the following number of cases where there are multiple disputes:

** lung disease: 2
** asthma: 3
** arth: 5
** ost: 7
** psych: 2 
** hyp: 4 
** angina: 7 
** heart attack: 3
** heart murmur: 2 
** diab: 4
** cholesterol: 2


/** lung disease **/	

	local idauniq =  116900
	
		/* reports diganosis in w2, disputes in w3 ("never had it") 
			but reports medication in w4. Reports diagnosis in w5, 
			then disputes in w6. Not in w7.
			This person is irritating. 
			reject all disputes, assume first onset was in w2. 
			no proxies. */
			
		replace everlu_w3 = 1 if idauniq == `idauniq' 
		replace everlu_w6 = 1 if idauniq == `idauniq' 
		
		forv i = 2/6 { 
			replace howcllu_w`i' = 8 if idauniq == `idauniq' 
		} 
			
	local idauniq =  117005
		
		/* reports diagnosis in w1 (+ medication), 
			disputes in w2, reports another diagnosis 
			in w3, missing in w4, disputes in w5 ("misdiagnosed") 
			no medication in w5. 
			be kind and accept their dispute. */ 
			
	forv i = 1/6 { 		
		replace everlu_w`i' = 0 if idauniq == `idauniq' 
		replace howcllu_w`i' = 6 if idauniq == `idauniq' 
	} 

	
/** asthma **/	
	
	local idauniq =  111763 
		/* reports diagnosis in w1, confirms in w2 
			(and reports medication), 
			disputes in w3 ("misdiagnosed"), 
			reports again in w4 (with medication), confirms in w5, 
			disputes in w6 ("never had it"). 
			Here the issue is when to assign the timing of the 
			diagnosis. 
			Wave 1 it is. **/ 
			
		forv i = 1/7 { 
			replace everas_w`i' = 1 if idauniq == `idauniq' 
			replace howclas_w`i' = 8 if idauniq == `idauniq' 
		}
	
	local idauniq = 119037
		/* reports diagnosis in wave 1 and confirms in waves 2 to 4
		disputes in wave 5 (never had it). reports again in wave 6.
		no medication reported.
		disputes in wave 7 (wrong diagnosis)
		Accept wrong diagnosis */
		
		forv i = 1/7 { 
			replace everas_w`i' = 1 if idauniq == `idauniq' 
			replace howclas_w`i' = 6 if idauniq == `idauniq' 
		}
	
	local idauniq = 161951
		/* reports diagnosis in wave 4 and confirms in wave 5.
		disputes in wave 6 (never had it) and in wave 7 (never had it).
		doesn't show as a feed-forward error...
		reports use of medication in wave 4. 
		reject dispute */
		
	forv i = 6/7 {	
		replace everas_w`i' = 1 if idauniq == `idauniq' 
		}
	
/** arthritis **/
	
	local idauniq =  107364
		/*  reports diagnosis in w1, 
			dispute in w2 ("didn't have it before, but have it now") 
			confirm in w3
			dispute in w4 ("never had it") 
			reject this person's claims. 
			(from w5 this person reports a diagnosis of dementia) */ 
			
		forv i = 1/7 { 
			replace everar_w`i' = 1 if idauniq == `idauniq' 
			replace howclar_w`i' = 7 if idauniq == `idauniq' 
		} 
	
	local idauniq =  107853 
		/*  reports diagnosis in w1 (six years ago),
			dispute in w2 ("didn't have it before, but have it now")
			dispute in w3 ("never had it")
			reject their dispute. 
			*/
	
		forv i = 1/7 { 
			replace everar_w`i' = 1 if idauniq == `idauniq' 
			replace howclar_w`i' = 7 if idauniq == `idauniq' 
		}
	
	local idauniq =  110891	
		/* reports diagnosis in w2 
			dispute in w3 ("didn't have it before, but have it now") 
			dispute in w4 ("misdiagnosed") 
			reports diagnosis in w7
			accept dispute of earlier diagnosis and set onset to w7 */
		
		forv i = 2/6 { 
			replace everar_w`i' = 0 if idauniq == `idauniq' 
			replace howclar_w`i' = 6 if idauniq == `idauniq' 
		} 
		
	local idauniq =  117119
		/* reports diagnosis in w4, disputes (never had) in w5.
		reports diagnosis in w6, disputes (never had) in w7.
		reject disputes */
		
		forv i = 5/7 {
		replace everar_w`i' =1 if idauniq == `idauniq'
		replace howclar_w`i' = 7 if idauniq == `idauniq' 
		}
		
	local idauniq = 118485
		/* Reports diagnosis in wave 1. Confirms in wave 2. 
		Disputes in wave 3 (never had). Reports diagnosis in wave 4.
		Confirms in waves 5 and 6. Disputes in wave 7.
		Reject disputes. */
	
		forv i = 2/7 {
		replace everar_w`i' =1 if idauniq == `idauniq'
		replace howclar_w`i' = 7 if idauniq == `idauniq'
		}
		
		
		
/** osteoporosis 
	no proxy respondents/dementia diagnoses **/ 
	
	local idauniq =  103964 
		/* reports diagnosis in w1, 
		disp in w2 ("Not prev but now") 
		disp in w3 ("Misdiagnosed") 
		accept */
		forv i = 1/6 { 
			replace everos_w`i' = 0 if idauniq == `idauniq' 
			replace howclos_w`i' = 6 if idauniq == `idauniq' 
		} 
		
	
	local idauniq =  104698
		/* reports diagnosis in w1, 
		confirms in w2 and w3 
		disp in w4 ("never had it") 
		seems to have been a ff error to w6, 
		disputes again, 'misdiagnosed'. 
		accept. */
		forv i = 1/6 { 
			replace everos_w`i'  = 0 if idauniq == `idauniq' 
			replace howclos_w`i' = 6 if idauniq == `idauniq' 
		} 
		
		
	local idauniq =  111700 
		/* reports diagnosis in w1, 
			dispute in w2 ("never had it")
			reports diagnosis in w5, disputes in w6 ("never had it")
			awkward. 
			reject. 
			*/
			
		forv i = 2/7 { 
			replace everos_w`i' = 1 if idauniq == `idauniq' 
			replace howclos_w`i' = 7 if idauniq == `idauniq' 
		} 
		
	local idauniq =  117939
		/* reports in w4, 
			dispute in w5 ("not before, but now")  
			dispute in w6 ("never had it") 
			reject. */
		forv i = 4/7 { 
			replace everos_w`i' = 1 if idauniq == `idauniq' 
			replace howclos_w`i' = 7 if idauniq == `idauniq' 
		}
				
		
	local idauniq =  118965
		/* reports diagnosis in w1, 
		disp in w2 ("never had it")
		reports in w3, 
		confirms in w4 and w5, 
		disputes in w6 ("misdiagnosis") 
		accept. */
		
		forv i = 1/7 { 
			replace everos_w`i' = 0 if idauniq == `idauniq' 
			replace howclos_w`i' = 6 if idauniq == `idauniq' 
		} 
				
		
	local idauniq =  118979
		/* reports in w3 
		   dispute in w4 ("not previously but now") 
		   dispute in w5 ("misdiagnosed") 
		   accept */
		   
		forv i = 3/7 { 
			replace everos_w`i' = 0 if idauniq == `idauniq' 
			replace howclos_w`i' = 6 if idauniq == `idauniq' 
		}
		
		
		
	local idauniq =  121050
		/* reports diagnosis in w1, 
		confirms in w2 and w3 
		dispute in w4 ("never had it") 
		reports in w5 (+ medication)
		disputes in w6 (misdiagnosed) 
		accept. */
		
		forv i = 1/7 { 
			replace everos_w`i' = 0 if idauniq == `idauniq' 
			replace howclos_w`i' = 6 if idauniq == `idauniq' 
		} 
			
	/* psychiatric problems */
		
		local idauniq =  120504
			/* diagnosis reported in w1, 
			   confirmation in w2
			   dispute in w3 ("not prev but now") 
			   confirm in w4 
			   dispute in w5 ("never had it")
			   
			   reject all disputes (diagnosis from w1) */ 
			   
			forv i = 1/7 { 
				replace everps_w`i' = 1 if idauniq == `idauniq' 
				replace howclps_w`i' = 7 if idauniq == `idauniq' 
			}
	
		local idauniq = 110996
			/* Diagnosis reported in w1. Disp w2 (never had).
			Diagnosis reported in w5, confirmed w6, disp w7 (misdiagnosis).
			Accept disputes. */
			
			forv i = 1/7 {
			replace everps_w`i' = 0 if idauniq == `idauniq' 
				replace howclps_w`i' = 6 if idauniq == `idauniq' 
			}
	
	
	/* hypertension */ 
		
	local idauniq =  108196 
		/* diagnosis reported in w1, 
		   not ff to w2, 
		   confirmed in w3, 
		   disp in w4 ("never had it")
		   ff error to w5, dispute in w5 (never had it)
		   reports medication for blood pressure in w1 w3 w4 w5 w6 
		   reject. */
		forv i = 1/7 { 
			replace everbp_w`i' = 1  if idauniq == `idauniq' 
			replace howclbp_w`i' = 8 if idauniq == `idauniq' 
		}
		   
		   
	local idauniq =  111699  
		/* diagnosis reported in w1, 
		dispute in w2 ("never had it") - measured as hypertensive in nurse visit
		diagnosis reported in w3
		dispute in w4 ("never had it")
		no medication reported. 
		reject. */
		
		forv i = 1/7 { 
			replace everbp_w`i' = 1 if idauniq == `idauniq' 
			replace howclbp_w`i' = 7 if idauniq == `idauniq' 
		}
				
	local idauniq =  117141 
		/* reports diagnosis in w3, 
			confirms in w4 (+medication), 
			dispute in w5 ("never had it") but also reports medication
			ff error(?) to w6, disputes again ("never had it") 
			reject. 
			*/
			
		forv i = 3/7 { 
			replace everbp_w`i' = 1 if idauniq == `idauniq' 
			replace howclbp_w`i' = 8 if idauniq == `idauniq' 
		}	
		
	local idauniq =  121002
		/* diagnosis reported in w1
			dispute in w2 ("Never had it") 
		   diagnosis reported in w3 
		    dispute in w4 ("never had it")
		   diagnosis reported in w7
			recorded as hypertensive in w0 and w4. 
			no medication. 
			reject disputes. */
			forv i = 1/7 { 
				replace everbp_w`i' = 1 if idauniq == `idauniq' 
				replace howclbp_w`i' = 7 if idauniq == `idauniq' 
			} 
			
			
/* 	angina */	
	
	local idauniq =  106279 
		/* report diagnosis in w1, 
		no ff to w2, 
		dispute in w3 ("never had it") 
		ff error (?) to w5, dispute in w5
		accept */
		forv i = 1/7  { 
			replace everan_w`i' = 0 if idauniq == `idauniq' 
			replace howclan_w`i' = 5 if idauniq == `idauniq' 
			
		}
			
		
	local idauniq =  107773
		/* report diagnosis in w1 , 
			no ff to w2 
			dispute in w3 ("never had it") 
			ff into w5 (fferror?) 
			dispute in w5 ("never had it") 
			accept. 
			*/
			
		forv i = 1/7 { 
			replace everan_w`i' = 0 if idauniq == `idauniq' 
			replace howclan_w`i' = 5 if idauniq == `idauniq' 
		} 
						
	local idauniq =  108619 
		/* reports diagnosis in w3, 
		 dispute in w4 ("misdiganosis") 
		 ff to w5 (error) and confirmed, 
		 then dispute in w6 (never had it) 
		 accept. */
		 
		forv i = 3/7 { 
			replace everan_w`i' = 0 if idauniq == `idauniq' 
			replace howclan_w`i' = 6 if idauniq == `idauniq' 
		} 		 
		 
	local idauniq =  110914 
		/* report diagnosis in w1, 
		confirm in w2 and w3  (medication in w3) 
		dispute in w4 ("never had it") 
		ff error to w5, dispute ("never had it") 
		reject. 		
		*/
		
		forv i = 1/7 { 
			replace everan_w`i' = 1 if idauniq == `idauniq' 
			replace howclan_w`i' = 8 if idauniq == `idauniq' 
		}
				
	local idauniq =  111568
		/* report diagnosis in w1, 
		dispute in w2 ("never had it") 
		ff error into w4, confirm, 
		confirm in w5, 
		dispute in w6 ("misdiagnosed"). 
		no medication ever. 
		accept. 
		*/
		forv i = 1/7 { 
			replace everan_w`i' = 0 if idauniq == `idauniq' 
			replace howclan_w`i' = 6 if idauniq == `idauniq' 
		} 
	
	local idauniq =  117826 
		/* report diagnosis in w1 , 
		no ff to w2 , 
		dispute in w3 ("misdiagnosed")
		ff error to w5, 
		dispute ("misdiagnosed") 
		accept. */
		forv i = 1/7 {
			replace everan_w`i' = 0 if idauniq == `idauniq' 
			replace howclan_w`i' = 6 if idauniq == `idauniq' 
		}
				
	local idauniq =  119953
		/* report diagnosis in w2,
		confirm in w3 
		dispute in w4 ("never had it")
		ff error into w5, dispute in w5 ("misdiagnosed") 
		accept. */
		forv i = 2/7 { 
			replace everan_w`i' = 0 if idauniq == `idauniq' 
			replace howclan_w`i' = 6 if idauniq == `idauniq' 
		} 
		
	/** heart attacks **/ 

	local idauniq =  106722 
		/* report diagnosis in w1 (when 38), 
		confirm in w2, 
		dispute in w3 ("never had it") 
		ff in w4 (error) 
		dispute in w4 ("never had it") 
		ordinarily would reject - but persistence of disputes 
		indicates ok. accept. */ 
		
		forv i = 1/7 { 
			replace evermi_w`i' = 0 if idauniq == `idauniq' 
			replace howclmi_w`i' = 5 if idauniq == `idauniq' 
		}
			
	local idauniq =  107291 
		/* reports diagnosis in w0, w1, 
		not ff to w2, disputes in w3 ("never had it") 
		ff to w4 (error) confirms, 
		ff to w5, disputes ("never had it") 
		one too many confirmations - reject. */
		forv i = 0/7 { 	
			replace evermi_w`i'  = 1 if idauniq == `idauniq' 
			replace howclmi_w`i' = 7 if idauniq == `idauniq' 
		}
	
	local idauniq = 105669
		/* ff error confirmed in w4. Confirmed w5.
		Disputed in w6 (not prev but now). Disputed in w7 (never had).
		Accept disputes. Already cleaned in ff error section. */
		
		
		
	
	/** heart murmur **/
		local idauniq =  119990 
			/* reports diagnosis in w1, 
			dispute in w2 ("never had it") 
			ff error to w5, dispute ("never had it") 
			accept. */
			forv i = 1/7 { 
				replace everhm_w`i' = 0 if idauniq == `idauniq' 
				replace howclhm_w`i' = 5 if idauniq == `idauniq' 
			}
			
		local idauniq =  120002 
			/* reports diagnosis in w1, 
			not ff to w2, 
			dispute in w3 ("never had it") 
			report diagnosis in w5, 
			dispute in w6 (misdiagnosed)
			accept. */ 
			
			forv i = 1/7 { 
				replace everhm_w`i'  = 0 if idauniq == `idauniq' 
				replace howclhm_w`i' = 5 if idauniq == `idauniq' 
			}
		
	
	/** diabetes **/	
		
	local idauniq =  104888
		/* diagnosis reported in w3, 
			missing in w4
			dispute in w5 "not prev but now" 
			dispute in w6 "misdiagnosed" 
			grudgingly, accept. */
		forv i = 3/7 { 
			replace everdi_w`i' = 0 if idauniq == `idauniq' 
			replace howcldi_w`i' = 6 if idauniq == `idauniq' 
		}			
			
	local idauniq =  116686 
		/* diagnosis reported in w1, 
			not ff to w2, 
			confirm in w3, 
			dispute in w4 ("not prev but now") 
			confirm in w5, 
			dispute in w6 "never had it" 
			reject. */ 
		forv i = 1/7 { 
			replace everdi_w`i' = 1 if idauniq == `idauniq' 
			replace howcldi_w`i' = 7 if idauniq == `idauniq' 
		}		
		
	local idauniq =  118512 
		/* diagnosis reported in w1, 
			not ff to w2, 
			dispute in w3 ("never had it") 
			report in w4 (+medication) 
			dispute in w5 ("misdiagnosed") 
			accept. */ 
		forv i = 0/7 { 
			replace everdi_w`i' = 0 if idauniq == `idauniq' 
			replace howcldi_w`i' = 6 if idauniq == `idauniq' 
		} 

	local idauniq = 105141
		/* Reports diagnosis in wave 3. Confirmed w4. 
		Dispute (never had it) w5. Reports diagnosis w6. 
		Disputes (never had it) w7. Reject disputes */
	forv i = 3/7 {
		replace everdi_w`i' = 1 if idauniq == `idauniq'
		replace howcldi_w`i' = 7 if idauniq == `idauniq' 
		}
		
	/** cholesterol **/
	
	local idauniq = 106737
	/* Reports diagnosis in wave 3. Confirms in w4 and 5.
	Dispute (never had it) in w6 and w7. Medication in w4 to w6.
	Reject disputes */
	
	forv i = 6/7 {
	replace everch_w`i' = 1 if idauniq == `idauniq'
	replace howclch_w`i' = 8 if idauniq == `idauniq'
	}
	
	local idauniq = 107614
	/* Reports diagnosis in wave 4.
	Dispute (never had it) in w5. FF error to wave 7 disputed (never had)
	Medication in w4. Together 2 reports so reject disputes */
	
	forv i = 5/7 {
	replace everch_w`i' = 1 if idauniq == `idauniq'
	replace howclch_w`i' = 8 if idauniq == `idauniq'
	}
	
		
/** make sure dispute decisions have been fed through to w0 **/	

foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	forv i = 1/6 { 
	di in red "`x' wave `i'" 
	assert  ever`x'_w0 != 1 if( howcl`x'_w`i' >=1 & howcl`x'_w`i' <=6 )
	}
	}
	
	save "$healthsave\disputes solved_w7.dta", replace 

	
	/** for documenation **/

		/** how have disputed been solved? **/	
			
		cd "P:\ELSA\Healthdynamics\documentation\tables\" 
		set more off 
		preserve 
		
		local tabout 
		
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 			
			replace anydisp`x' = . if ffproblem`x' == 1 
			egen howcl`x' = rowmax(howcl`x'_w?) 
			replace howcl`x' = . if howcl`x'<5 | howcl`x'>9 
			
			gen `x' = . 
			forv i = 5/9 { 
				su howcl`x' if howcl`x' == `i'  
				replace `x' = `r(N)' if _n==`i'-4 
			} 
			local tabout `tabout' mean `x'
		}
		gen str label = "Dispute accepted (1 report)" if _n==1 
		replace label = "Misdiagnosis" if _n==2 
		replace label = "Repeat offender" if _n==3
		replace label = "Medication" if _n== 4
		replace label = "Apparent psychic" if _n==5 
		
		tabout label using how_disputes_solved.txt, ///
				c(`tabout') replace sum   
		restore 
		
	/** compare prevalence rates of cleaned vars and naive vars **/	
		preserve 
		keep idauniq inwave* ever??_w? 
		ren ever??_w? clever??_w? 
		merge 1:1 idauniq using "$healthsave\basic healthvars with ff errors solved_w7.dta"
		
		ren *_w? *? 
		
		local foreshape 
		local fortabout 
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 			
			forv i = 1/6 { 
				replace ever`x'`i' = . if ever`x'`i' ==-1 
				replace clever`x'`i' = . if clever`x'`i' == -1 
			}
			local foreshape `foreshape' ever`x' clever`x'
			local fortabout `fortabout' mean ever`x' mean clever`x'
		} 	
		ren inwave inwaveall 		
		reshape long `foreshape' inwave , i(idauniq) j(wave) 
				
		keep if inwave==1 
		
		tabout wave using ff_solved_and_post_disp_cleaning_lifetime_prevalence_rates.txt, ///
			c(`fortabout') f(6) sum replace 
		tabout wave if inwaveall==16 using bal_ff_solved_and_post_disp_cleaning_lifetime_prevalence_rates.txt, ///
			c(`fortabout') f(6) sum replace 
		restore 	
		
		
